import * as path from 'path'
import fse from 'fs-extra'
import vue from 'rollup-plugin-vue'
import commonjs from "@rollup/plugin-commonjs"
import image from "@rollup/plugin-image"
import { terser } from 'rollup-plugin-terser'
import resolve from "@rollup/plugin-node-resolve"
import postcss from 'rollup-plugin-postcss'
import gzipPlugin from 'rollup-plugin-gzip'
import replace from 'rollup-plugin-replace'
import copy from 'rollup-plugin-copy'
const isPro = process.env.NODE_ENV === 'production'
// 组件名称，可以根据名称，个性化修改打包配置
const comName = process.env.COMPONENT_NAME
const cwd = process.cwd()

// 清空目标目录
fse.emptyDirSync(path.join(cwd, `dist/${comName}`))

const config = [
  {
    input: path.join(__dirname, `src/components/${comName}/index.js`),
    output: [
      {
        format: 'umd',
        globals: {
          vue: "Vue" // 告诉rollup全局变量Vue即是vue
        },
        file: `dist/${comName}/index.umd.js`,
        name: "MicroComponent"
      }
    ],
    plugins: [
      vue({
        // Dynamically inject css as a <style> tag
        css: true,
        // Explicitly convert template to render function
        compileTemplate: true
      }),
      replace({
        'process.env.NODE_ENV': JSON.stringify('development'),
        'process.env.VUE_ENV': JSON.stringify('browser')
      }),
      postcss(),
      resolve(),
      commonjs(),
      image(),
      terser({
        ecma: 5,
      }),
      copy({
        targets: [
          {
            src: `src/components/${comName}/static/**/*`,
            dest: `dist/${comName}/static`
          }
        ]
      })
    ]
  }
]
if (isPro) {
  config[0].plugins.push(gzipPlugin())
}
module.exports = config
